"use strict";
{
    class Node {
        constructor(element = null, previous = null, next = null) {
            this.element = element;
            this.previous = previous;
            this.next = next;
        }
    }
    class LinkList {
        constructor() {
            this.head = new Node("head");
        }
        find(item) {
            let current = this.head;
            while (current.next) {
                if (current.element === item) {
                    break;
                }
                else {
                    current = current.next;
                }
            }
            return current;
        }
        findPrevious(item) {
            let current = this.head;
            while (current.next) {
                if (current.next.element === item) {
                    break;
                }
                else {
                    current = current.next;
                }
            }
            return current;
        }
        findLast() {
            let curent = this.head;
            while (curent.next) {
                curent = curent.next;
            }
            return curent;
        }
        insert(element, item) {
            let current = this.find(item);
            let newNode = new Node(element);
            if (current.next) {
                // 双向链表插入元素
                current.next.previous = newNode;
                newNode.next = current.next;
                newNode.previous = current;
                current.next = newNode;
            }
            else {
                current.next = newNode;
                newNode.previous = current;
            }
        }
        remove(item) {
            let currNode = this.find(item);
            if (!(currNode.next == null) && !(currNode.previous == null)) {
                currNode.previous.next = currNode.next;
                currNode.next.previous = currNode.previous;
                currNode.next = null;
                currNode.previous = null;
            }
        }
        display() {
            var _a;
            let currNode = this.head;
            // console.log(currNode);
            while (currNode) {
                console.log(currNode.element, (_a = currNode.previous) === null || _a === void 0 ? void 0 : _a.element);
                if (currNode.next) {
                    currNode = currNode.next;
                }
                else {
                    break;
                }
            }
        }
        dispReverse() {
            let last = this.findLast();
            // console.log(last.element);
            while (last) {
                console.log(last.element);
                if (last.previous) {
                    last = last.previous;
                }
                else {
                    break;
                }
            }
        }
    }
    let doubleLink = new LinkList();
    doubleLink.insert("jimmy", "head");
    doubleLink.insert("xuexue", "jimmy");
    doubleLink.insert("henry", "xuexue");
    doubleLink.insert("jack", "henry");
    // doubleLink.remove("xuexue");
    doubleLink.display();
    console.log("*************************");
    doubleLink.dispReverse();
}
